package com.dx.mes.common.transcoder.util;

import java.io.*;

public class ObjectTranscoder { 
	public static byte[] serialize(Object value) throws IOException{
        byte[] rv=null;  
        ByteArrayOutputStream bos = null;  
        ObjectOutputStream oos = null;  
        try {  
            bos = new ByteArrayOutputStream();  
            oos = new ObjectOutputStream(bos);
            oos.writeObject(value);  
            rv = bos.toByteArray();  
        } catch (IOException e) {  
        	throw new IllegalArgumentException("Non-serializable object", e); 
        } finally {  
        	oos.close();  
            bos.close(); 
        }  
        return rv;  
    }  

    public static Object deserialize(byte[] in) throws IOException {  
        Object rv = null;  
        ByteArrayInputStream bis = null;  
        ObjectInputStream is = null;  
        try {  
            bis = new ByteArrayInputStream(in);  
            is = new ObjectInputStream(bis);  
            rv = is.readObject();  
        } catch (IOException e) {
        	throw new IllegalArgumentException("Caught IOException decoding " 
        			+ (in == null ? 0 : in.length)
            		+ " bytes of data" + e.toString());
        } catch (ClassNotFoundException e) {
         	throw new IllegalArgumentException("Caught CNFE decoding " 
         			+ (in == null ? 0 : in.length)
            		+ " bytes of data" + e.toString());
        } finally {  
            is.close();
            bis.close();
        }  
        return rv;  
    }
}
